package il.ac.technion.cs236700.address_book;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * all usages of a group name in this calls requires the name to be fully qualified path,
 * like file system navigation, e.g - if the group name is "Office" and you want 
 * to add a new group called "Marketing" under "Office", groupName should be "Office/Marketing"
 * 
 * @authors Regev Azriel & Sharon Ronen
 *
 */
public class Group {
	
	String name;
	ArrayList<Contact> contacts;
	
	public Group(String name){
		this.contacts = new ArrayList<Contact>();
		this.name = name;
	}
	
	public Group(String name,ArrayList<Contact> contacts) {
		this.contacts = contacts;
		this.name = name;
	}
	
	/**
	 * quantified group name
	 * @return the name of this group
	 */
	public String groupName(){
		return name;
	}
	
	/**
	 * overriding the method equals
	 */
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Group)) return false;
		return name.equals(((Group)obj).groupName());
	}
	
	/**
	 * checks if the group can be an ancestor of groupName according its name
	 * @param groupName - fully qualified name
	 * @return - true if the group's name is part of the new path in groupName
	 * for example: for groupName = Office/HR/David,group's name=Office returns true,
	 * for groupName = Office/HR/David,group's name=Finance returns false
	 */
	public boolean canBeAncestorOf(String groupName) {
		return Arrays.asList(groupName.split("/")).contains(name);		
	}

	/**
	 * checks if the group can be a direct parent of groupName according its name
	 * @param groupName - fully qualified name
	 * @return - true if the group's name is the last directory before the
	 * new group's name,for example: for groupName = Office/HR/David,group's name=HR returns true
	 * but for groupName = Office/HR/David,group's name=Office returns false
	 * 
	 */
	public boolean canBeParentOf(String groupName) {
		String[] $ = groupName.split("/");
		return $.length>1?$[$.length-2].equals(name):false;
	}
	
	@Override
	public String toString() {
		return name.split("/")[name.split("/").length-1].toString();
	}

	public boolean contains(Person p) {
		for (Contact c : contacts) {
			if (c.contains(p)) return true;
		}
		return false;
	}

	public void setGroupName(String groupName) {
		this.name = groupName;
	}
	
}
